package main

import (
    "fmt"
    "math/rand"
    "time"
)

// GameCharacter 游戏角色接口
type GameCharacter interface {
    Attack() int
    Defend() int
    GetCharacterType() string
    GetStats() string
    LevelUp()
}

// Warrior 战士
type Warrior struct {
    Name     string
    Level    int
    Strength int
    Defense  int
    Health   int
}

func (w *Warrior) Attack() int {
    damage := w.Strength * 2 + rand.Intn(10)
    fmt.Printf("战士 %s 发动攻击，造成 %d 点伤害\n", w.Name, damage)
    return damage
}

func (w *Warrior) Defend() int {
    defense := w.Defense + rand.Intn(5)
    fmt.Printf("战士 %s 进行防御，减少 %d 点伤害\n", w.Name, defense)
    return defense
}

func (w *Warrior) GetCharacterType() string {
    return "战士"
}

func (w *Warrior) GetStats() string {
    return fmt.Sprintf("战士 %s - 等级:%d, 力量:%d, 防御:%d, 生命:%d", 
        w.Name, w.Level, w.Strength, w.Defense, w.Health)
}

func (w *Warrior) LevelUp() {
    w.Level++
    w.Strength += 3
    w.Defense += 2
    w.Health += 20
    fmt.Printf("战士 %s 升级到 %d 级\n", w.Name, w.Level)
}

// Mage 法师
type Mage struct {
    Name        string
    Level       int
    Intelligence int
    MagicPower  int
    Mana        int
}

func (m *Mage) Attack() int {
    damage := m.Intelligence * 3 + rand.Intn(15)
    fmt.Printf("法师 %s 释放魔法，造成 %d 点魔法伤害\n", m.Name, damage)
    return damage
}

func (m *Mage) Defend() int {
    defense := m.MagicPower/2 + rand.Intn(3)
    fmt.Printf("法师 %s 使用魔法护盾，减少 %d 点伤害\n", m.Name, defense)
    return defense
}

func (m *Mage) GetCharacterType() string {
    return "法师"
}

func (m *Mage) GetStats() string {
    return fmt.Sprintf("法师 %s - 等级:%d, 智力:%d, 魔力:%d, 法力:%d", 
        m.Name, m.Level, m.Intelligence, m.MagicPower, m.Mana)
}

func (m *Mage) LevelUp() {
    m.Level++
    m.Intelligence += 4
    m.MagicPower += 3
    m.Mana += 30
    fmt.Printf("法师 %s 升级到 %d 级\n", m.Name, m.Level)
}

// CharacterFactory 角色工厂
type CharacterFactory struct{}

// CreateCharacter 创建游戏角色
func (f *CharacterFactory) CreateCharacter(characterType string, name string) (GameCharacter, error) {
    switch characterType {
    case "warrior":
        return &Warrior{
            Name:     name,
            Level:    1,
            Strength: 15, 
            Defense:  12,
            Health:   100,
        }, nil
    case "mage":
        return &Mage{
            Name:         name,
            Level:        1,
            Intelligence: 18,
            MagicPower:   20,
            Mana:         80,
        }, nil
    default:
        return nil, fmt.Errorf("不支持的角色类型: %s", characterType)
    }
}

// 使用演示
func main() {
    fmt.Println("=== 角色工厂演示 ===")
    rand.Seed(time.Now().UnixNano())
    
    factory := &CharacterFactory{}
    
    // 创建不同类型的角色
    warrior, _ := factory.CreateCharacter("warrior", "阿尔萨斯")
    mage, _ := factory.CreateCharacter("mage", "吉安娜")
    
    characters := []GameCharacter{warrior, mage}
    
    for _, char := range characters {
        fmt.Println(char.GetStats())
        char.Attack()
        char.Defend()
        char.LevelUp()
        fmt.Println(char.GetStats())
        fmt.Println()
    }
}